import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-26
 * Time:14:10
 */
public class MyArrayList implements ILIst{
    public int[] array;
    public int usedSize;//未初始化默认为0

    public static final int DEFAULT_CAPACITY = 10;//规定数组的默认容量

    public MyArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public boolean isFull() {
        return this.usedSize == array.length;
    }
    @Override
    public void grow() {
        this.array = Arrays.copyOf(this.array,2*this.array.length);
    }

    //查看Pos位置是否有效
    private void checkPos(int pos) throws PosIllegalException{
        if (pos < 0 || pos > usedSize) {
            throw new PosIllegalException("Pos位置不合法！");
        }
    }
    @Override
    public void add(int date) {
        if (isFull()) {
            grow();
        }
        this.array[this.usedSize] = date;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int date) {
        try {
            checkPos(pos);
            if (isFull()) {
                grow();
            }
            //挪动元素
            for (int i = this.usedSize-1; i >= pos; i--) {
                array[i+1] = array[i];
            }
            array[pos] = date;
            usedSize++;
        }catch (PosIllegalException e) {
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }
    }

    /*
    包含 toFind 返回下标，否则返回 -1
     */
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            checkPos2(pos);

            return array[pos];

        }catch (PosIllegalException e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return -1;
    }
    private void checkPos2(int pos) throws PosIllegalException{
        if (pos < 0 ||  pos >= usedSize) {
            throw new PosIllegalException("Pos位置不合法！");
        }
    }
    @Override
    public boolean isEmpty() {
        return usedSize == 0;
    }
    private void checkEmpty() {
        if (isEmpty()) {
            throw new EmptyException("顺序表为空！");
        }
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos2(pos);

            array[pos] = value;

        }catch (PosIllegalException e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1) {
                return;
            }
            for (int i = pos; i < usedSize-1; i++) {
                array[i] = array[i+1];
            }
            usedSize--;

        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void clear() {
        /*for (int i = 0; i < usedSize; i++) {
            array[i] = null;      —————— 如果数组中的元素是一个引用类型，则需要这样写，将地址置为null，防止内存泄露
        }*/
        usedSize = 0; // —————— 数组中没有引用类型，因此只需直接将其置为0即可
        //将usedSize设置为0后，顺序表在逻辑上就被清空了。因为后续的访问操作（如get、remove等）都会基于usedSize的值，
        //而usedSize为0表示当前没有元素。
    }


    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
